import os

from django.conf import settings
from django.template import loader

from celery_tasks.main import celery_app
from collections import OrderedDict

from goods.models import GoodsChannel, SKU
from goods.utils import get_categories


@celery_app.task
def generate_static_list_search_html():
    """
    生成静态的商品列表页和搜索结果页html文件
    """
    # 商品分类菜单
    categories = get_categories()

    context = {
        'categories': categories
    }
    template = loader.get_template('list.html')
    html_txt = template.render(context)
    file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, 'list.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_txt)

    template = loader.get_template('search.html')
    html_txt = template.render(context)
    file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, 'search.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_txt)


@celery_app.task
def generate_static_sku_detail_html(sku_id):
    """生成静态的商品详情页html文件"""

    # 商品分类菜单
    categories = get_categories()

    # 当前sku商品信息
    sku = SKU.objects.get(id=sku_id)
    sku.images = sku.skuimage_set.all()

    # 构造面包屑的频道信息
    goods = sku.goods
    goods.channel = goods.category1.goodschannel_set.all()[0]

    # 构造sku信息
    # 获取当前商品的规格信息
    sku_key = []
    sku_specs = sku.skuspecification_set.order_by('spec_id')
    for sku_spec in sku_specs:
        sku_key.append(sku_spec.option.id)

    # 获取当前商品的所有sku
    skus = goods.sku_set.all()

    # 构建当前商品的规格信息字典
    # spec_sku_map = {
    #     (规格1参数id, 规格2参数id, 规格3参数id, ...): sku_id,
    #     (规格1参数id, 规格2参数id, 规格3参数id, ...): sku_id,
    #     ...
    # }
    spec_sku_map = {}
    for s in skus:
        # sku的所有规格信息参数
        s_specs = s.skuspecification_set.order_by('spec_id')
        key = []
        for s_spec in s_specs:
            key.append(s_spec.option.id)

        spec_sku_map[tuple(key)] = s.id

    # 构造当前商品的规格信息
    # specs = [
    #    {
    #        'name': '屏幕尺寸',
    #        'options': [
    #            {'value': '13.3寸', 'sku_id': xxx},
    #            {'value': '15.4寸', 'sku_id': xxx},
    #        ]
    #    },
    #    {
    #        'name': '颜色',
    #        'options': [
    #            {'value': '银色', 'sku_id': xxx},
    #            {'value': '黑色', 'sku_id': xxx}
    #        ]
    #    },
    #    ...
    # ]
    specs = goods.goodsspecification_set.order_by('id')
    for index, spec in enumerate(specs):
        key = sku_key[:]
        options = spec.specificationoption_set.all()
        for option in options:
            key[index] = option.id
            option.sku_id = spec_sku_map.get(tuple(key))

        spec.options = options

    # 渲染模板
    context = {
        'categories': categories,
        'goods': goods,
        'sku': sku,
        'specs': specs
    }

    template = loader.get_template('detail.html')
    html_txt = template.render(context)
    file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, 'goods/' + str(sku_id) + '.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_txt)
